<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/content/themes/gray/easyui.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/content/themes/icon/icon.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/content/css/Index.css" />
<script src="${pageContext.request.contextPath}/script/Jquery/jquery-1.11.0.min.js"></script>
<script src="${pageContext.request.contextPath}/script/Easyui/jquery.easyui.min.js"></script>
<script src="${pageContext.request.contextPath}/script/Easyui/easyui-lang-zh_CN.js"></script>
<script src="${pageContext.request.contextPath}/script/Common/utils.js"></script>
<script src="${pageContext.request.contextPath}/script/Common/common.js"></script>
<script src="${pageContext.request.contextPath}/script/Common/index.js"></script>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>教师信息表</title>
<script type="text/javascript">
	$(function() {
		$("#tbList").datagrid({
			url : 'TeacherListServlet',
			loadMsg : '数据加载中请稍后……',
			pagination : true,
			rownumbers : true,
			idField : "ID",
			lines : true,
			columns : [ [ {
				field : 'Name',
				title : '教师姓名',
				sortable : true,
				width : 100,
				align : "center",
				editor:'textbox'
			}, {
				field : 'Gender',
				title : '性别',
				sortable : true,
				width : 100,
				align : "center",
				editor:{
					type:'combobox',
					options:{
						requird:true,
						editable:false,
						data:[{
							'id':'男','text':'男'},
							{'id':'女','text':'女'
						}],
						valueField:'id',
						textField:'text'
					}
				}
			}, {
				field : 'Birthday',
				title : '出生日期',
				sortable : true,
				width : 80,
				align : "center",
				editor:{
					type:'datebox',
					options:{
						required:true
					}
				}
			}, {
				field : 'NativePlace',
				title : '籍贯',
				align : "center",
				editor:'textbox'
			}, {
				field : 'Tel',
				title : '电话',
				sortable : true,
				width : 200,
				align : "center",
				editor:'textbox'
			}, {
				field : 'Email',
				title : 'E-mail',
				sortable : true,
				width : 80,
				align : "center",
				editor:'textbox'
			}, {
				field : 'QQ',
				title : 'QQ',

				width : 80,
				align : "center",
				editor:'textbox'
			} ] ],
			singleSelect : true,
			pageSize : 20,
			pageList : [ 20, 50 ],
			striped : true,
			onAfterEdit: function (rowIndex, rowData, changes) {
                 if (editAction == "edit") {
                   	$.post('TeacherListServlet', {
                   		method:"edit",
                   		Id:rowData.Id,
                   		Name:rowData.Name,
                   		Gender:rowData.Gender,
                   		Birthday:rowData.Birthday,
                   		NativePlace:rowData.NativePlace,
                   		Tel:rowData.Tel,
                   		Email:rowData.Email,
                   		QQ:rowData.QQ
                   		},
                   		function(data,textStatus,xhr){
                    			$("#tbList").datagrid("reload");
                    		}
                   	);	
                 } else if (editAction == "add") {
                   	$.post('TeacherListServlet', {
                   		method:"add",
                   		Name:rowData.Name,
                   		Gender:rowData.Gender,
                   		Birthday:rowData.Birthday,
                   		NativePlace:rowData.NativePlace,
                   		Tel:rowData.Tel,
                   		Email:rowData.Email,
                   		QQ:rowData.QQ
                   		},
                   		function(data,textStatus,xhr){
                    			$("#tbList").datagrid("reload");
                    		}
                   	);	
                 }
                 editRow = undefined;
             }, onCancelEdit: function (rowIndex, rowData) {
                 $("#tbList").datagrid("rejectChanges");
             }
		});
	})
	
</script>
</head>
<body>
	<table id="tbList" class="easyui-datagrid" title="教师信息表" style="width: 100%" fit="true" data-options="
				iconCls: 'icon-edit',
				toolbar: '#tb',
				method: 'post',
			">
	<div id="tb" style="height:auto">
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="funcAdd()">添加</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="funcEdit()">修改</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="funcRemove()">删除</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="funcSave()">保存</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="funcCancel()">取消</a>
		&nbsp;姓名：&nbsp;<input type="text" name="name" id="name" size="10"/><a href="javascript:funcSearch()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
		 <a href="javascript:void(0)" id="Submit" class="easyui-linkbutton" onclick="funcReload()" data-options="iconCls:'icon-reload',plain:true">重置</a> &nbsp;&nbsp;
	</div>
	</table>
	<script type="text/javascript">
		 editRow = undefined;
	     editAction = undefined;
	     function funcAdd() {
	            //添加时先判断是否有开启编辑的行，如果有则把开户编辑的那行结束编辑
	            if (editRow != undefined) {
					return;
	            }
	            //添加时如果没有正在编辑的行，则在datagrid的第一行插入一行
	            if (editRow == undefined) {
	                $("#tbList").datagrid("insertRow", {
	                    index: 0, // index start with 0
	                    row: {
	                    	
	                    }
	                });
	                //将新插入的那一行开户编辑状态
	                $("#tbList").datagrid("beginEdit", 0);
	                //给当前编辑的行赋值
	                editRow = 0;
	                editAction = "add";
	            }
	        }
	        function funcEdit() {
	            var row = $('#tbList').datagrid('getSelected');
	            if (row) {
	                if (editRow != undefined) {return;}
	                else {
	                    var index = $("#tbList").datagrid('getRowIndex', row);
	                    //   var index = row.Id;
	                    $("#tbList").datagrid('beginEdit', index);
	                    editAction = "edit";
	                    editRow = index;
	                }
	            }
	            else {
	                com.message("information", "请选中一行");
	            }
	        }
	        function funcRemove() {
	        	if(editRow != undefined){return;}
	            $.messager.confirm('确认对话框', '您确定删除该行吗？', function (r) {
	                if (r) {
	                	editAction = "remove";
	                    var rowData = $("#tbList").datagrid("getSelected");
	                    if (rowData) {
	                    	$.post('TeacherListServlet', {
	                    		method:"remove",
	                    		Id:rowData.Id
	                    		},
	                    		function(data,textStatus,xhr){
	                     			$("#tbList").datagrid("reload");
	                     		}
	                    	);	
	                    }
	                }
	            });
	        } 

	        function funcSave() {
	            $('#tbList').datagrid('endEdit', editRow);
	        }
	        function funcCancel() {
	            $('#tbList').datagrid("cancelEdit", editRow);
	            editRow = undefined;
	            editAction = undefined;
	        }
	        
			function funcSearch() {
				$('#tbList').datagrid('load', {
					name : $('#name').val()
				});
			}
			
			function funcReload(){
				$('#tbList').datagrid('load',{
					name : null
				});
			}
	</script>
	
</body>
</html>